<template>
  <Form autocomplete="off" class="user-form" ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
    <!-- fake fields are a workaround for chrome autofill getting the wrong fields -->
    <FormItem v-if="isAdd" label="所属单位" prop="orgId">
      <TreeSelect filterable width="100%" :data="orgList"  v-model="formValidate.orgId" placeholder="请选择所属单位"></TreeSelect>
    </FormItem>
    <FormItem label="项目名称" prop="name" >
      <Input :disabled="!isAdd"  name="name"  v-model="formValidate.name" placeholder="请输入项目名称"></Input>
    </FormItem>
    <FormItem v-if="isAdd" label="项目编码" prop="code" >
      <Input  name="name"  v-model="formValidate.code" placeholder="请输入项目名称"></Input>
    </FormItem>
    <FormItem label="城市区域" prop="regionCode">
      <Cascader filterable width="100%" :data="list"  v-model="formValidate.regionCode" placeholder="请选择城市区域"></Cascader>
    </FormItem>
    <FormItem label="项目地址" prop="address">
      <Input v-model="formValidate.address"   placeholder="请输入项目地址"></Input>
    </FormItem>
    <FormItem label="成立时间" prop="regdate">
      <DatePicker v-model="formValidate.regdate" type="date" placeholder="请选择时间" ></DatePicker>
    </FormItem>
    <FormItem label="项目描述" prop="bz">
      <Input v-model="formValidate.bz" type="textarea"  placeholder="请输入项目描述"></Input>
    </FormItem>
    <div class="ivu-text-center mt20">
      <Button  @click="handleSubmit('formValidate')">提交</Button>
<!--      <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>-->
    </div>
  </Form>
</template>
<script>
import {projectAdd,projectUpdate} from '@/api/project';
export default {
  created() {

  },
  props:{
    list:{
      type:Array,
      default:()=>{
        return []
      }
    },
    orgList:{
      type:Array,
      default:()=>{
        return []
      }
    },
    isAdd:{
      type:Boolean,
      default:true,
    },
    editorData:{
      type:Object,
      default:()=>{
        return {};
      }
    },
  },
  watch:{
    isAdd(newdata,oldData){
        this.init();
    }
  },
  mounted() {
    this.init()
  },
  data () {
    return {
      pwdType:'text',
      formValidate: {
        name:'',
        code:'',
        bz:'',
        address:'',
        id:'',
        regdate:'',
        orgId:'',
      },
      ruleValidate: {
        orgId: [
          { required: true,  message: '项目所属单位不能为空', trigger: 'change' },
        ],
        name: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' },
          { max: 100, message: '项目名称长度不能超过100个字符', trigger: 'blur' },
        ],
        address: [
          { max: 100, message: '项目地址长度不能超过100个字符', trigger: 'blur' },
        ],
        bz: [
          { max: 255, message: '项目备注长度不能超过255个字符', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '项目编码不能为空', trigger: 'blur' },
          { max: 100, message: '项目编码长度不能超过100', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    init(){
      console.log(this.editorData,'this.editorData')
      this.formValidate=this.isAdd?{
        name:'',
        code:'',
        bz:'',
        address:'',
        id:'',
        regdate:'',
        orgId:'',
        reginCode:[],
      }:{...this.editorData}
    },
    getSumbitParm(){
      return {
        ...this.formValidate,
        province :_.get(this.formValidate,'regionCode.0')||'',
        city:_.get(this.formValidate,'regionCode.1')||'',
        district:_.get(this.formValidate,'regionCode.2')||'',
      }
    },
    async Add(){
      return  await projectAdd(this.getSumbitParm());
    },
    async updata(){
      return await projectUpdate(this.getSumbitParm())
    },
    handleSubmit (name) {
      this.$refs[name].validate(async (valid) => {
        if (valid) {
        let res = this.isAdd? await this.Add():await this.updata();
          if(_.get(res,'code')==0){
            this.$Message.success(this.isAdd?'新增成功':'修改成功');
            this.$emit('success');
          }else{
            this.$Message.error(_.get(res,'message'));
          }

        } else {
          // this.$Message.error('Fail!');
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields();
    }
  }
}
</script>
<style scoped lang="less">

</style>
